package org.jeecg.modules.system.rule;

import com.alibaba.fastjson.JSONObject;
import org.jeecg.common.handler.IFillRuleHandler;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.modules.system.service.ISysDepartService;


public class SupplierMainCodeRule implements IFillRuleHandler {

    ISysDepartService sysDepartService = (ISysDepartService) SpringContextUtils.getBean("sysDepartServiceImpl");
    @Override
    public Object execute(JSONObject params, JSONObject formData) {

        String prefix = "GYS";
        //订单前缀默认为CN 如果规则参数不为空，则取自定义前缀
        if (params != null) {
            Object obj = params.get("prefix");
            if (obj != null) prefix = obj.toString();
        }
        int orderNumber =getNextOrderNumber1(); // 需要实现此方法来获取下一个订单号

        // 格式化订单号
        String value = String.format("%s%07d", prefix, orderNumber);

        return value;
    }
    private int getNextOrderNumber1() {
        Integer maxOrderNumber = sysDepartService.getMaxOrderNumber1(); // 从 Mapper 获取最大值
        return (maxOrderNumber != null ? maxOrderNumber + 1 : 1); // 如果最大值为 null，返回 1
    }
}